package library.net.retrofit.entity;

import android.os.Environment;
import android.util.Log;

import java.io.File;

import library.common.util.SharedUtils;
import library.net.retrofit.RxContextType;

/**
 * 文件下载描述，用于文件存储，断点续传
 *
 * @author Created by qiang.hou on 2017/12/18.
 * @version 1.0
 */
public class FileEntity {

    private final static String dest_file_dir = "dest_file_dir";
    private final static String dest_file_name = "dest_file_name";
    private final static String dest_complete_bytes = "dest_complete_bytes";
    private final static String dest_total_bytes = "dest_total_bytes";
    private final static String DEFAULT_DEST_FILE_DIR = Environment.getExternalStorageDirectory().getPath();
    /**
     * 远程下载地址
     */
    public String url;
    /**
     * 目标文件存储的文件夹路径
     */
    public String destFileDir = DEFAULT_DEST_FILE_DIR;
    /**
     * 目标文件存储的文件名
     */
    public String destFileName;

    /**
     * 已经下载的文件字节数（用于断点续传）
     */
    public Long completedBytes;

    public long totalBytes;

    /* 计算网络传输速度（byte/秒） */
    public long networkSpeed = 0L;

    /**
     * get file message from SharedPreference
     *
     * @param url remote url
     */
    public FileEntity initFromSharedPre(String url) {
        destFileDir = SharedUtils.getString(dest_file_dir + url, DEFAULT_DEST_FILE_DIR);
        destFileName = SharedUtils.getString(dest_file_name + url, null);
        completedBytes = SharedUtils.getLong(dest_complete_bytes + url, 0);
        totalBytes = SharedUtils.getLong(dest_total_bytes + url, 0);
        this.url = url;
        return this;
    }

    /**
     * save file message to SharedPreference
     *
     * @param url remote url
     */
    public void commitToSharedPre(String url) {
        if (null == url || url.equals("")) {
            return;
        }
        if (this.totalBytes == this.completedBytes) {
            SharedUtils.setLong(FileEntity.dest_complete_bytes + url, 0);
            SharedUtils.setLong(FileEntity.dest_total_bytes + url, 0);
        } else {
            SharedUtils.setLong(FileEntity.dest_complete_bytes + url, completedBytes);
            SharedUtils.setLong(FileEntity.dest_total_bytes + url, totalBytes);
        }
        SharedUtils.setString(FileEntity.dest_file_dir + url, destFileDir);
        SharedUtils.setString(FileEntity.dest_file_name + url, destFileName);

    }

    /**
     * 返回本地文件保存路径 + 文件名
     *
     * @param type content-type
     * @return file save path: dir + name
     */
    public static String initSavePath(FileEntity fileEntity, String type) throws Exception {
        if (null == fileEntity) {
            throw new Exception("no file local desc");
        }
        if (null == fileEntity.destFileName || fileEntity.destFileName.equals("")) {
            Log.i("initSavePath", "contentType:>>>>" + type);
            String fileSuffix;
            /* 自定义数据类型 */
            switch (type) {
                case RxContextType.APK_DATA:
                    fileSuffix = ".apk";
                    break;
                case RxContextType.IMAGE_PNG:
                    fileSuffix = ".png";
                    break;
                case RxContextType.IMAGE_JPG:
                    fileSuffix = ".jpg";
                    break;
                case RxContextType.TEXT_DATA:
                    fileSuffix = ".txt";
                    break;
                default:
                    fileSuffix = ".default";
                    break;
            }
            fileEntity.destFileName = System.currentTimeMillis() + fileSuffix;
        }
        String savePath = fileEntity.destFileDir.endsWith(File.separator) ?
                fileEntity.destFileDir + fileEntity.destFileName :
                fileEntity.destFileDir + File.separator + fileEntity.destFileName;
        Log.i("initSavePath", "savePath:>>>>" + savePath);
        return savePath;
    }
}
